package com.cn.onetrip.untility;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.cn.onetrip.activity.IndexLoadingActivity;
import com.cn.onetrip.activity.TripActivity;
import com.cn.onetrip.objects.ArticleObj;
import com.cn.onetrip.objects.AtlasObj.PictureObj;
import com.cn.onetrip.objects.AdvertsObj;
import com.cn.onetrip.objects.ArticleListObj;
import com.cn.onetrip.objects.ContentObj;
import com.cn.onetrip.objects.InfosListObj;
import com.cn.onetrip.objects.LarkObj;
import com.cn.onetrip.objects.NotesListObj;
import com.cn.onetrip.objects.PointAudioObj;
import com.cn.onetrip.objects.RouteObj;
import com.cn.onetrip.objects.RoutePointObj;
import com.cn.onetrip.objects.VideosMediaObj;
import com.cn.onetrip.objects.VisitsListObj;
import com.cn.onetrip.objects.WeatherObj;

import android.content.Context;
import android.graphics.Point;

public class ParsonJsonUtility {

	public Context mContext;
	public SysApplication sys;
	public boolean isOfflineData = IndexLoadingActivity.isOfflineData;
	public String OfflineFolder = "";

	public ParsonJsonUtility(Context context) {
		this.mContext = context;
		this.sys = new SysApplication(context);
		isOfflineData = IndexLoadingActivity.isOfflineData;
	}

	public void parsonJsonWeather(String weather) {
		WeatherObj weatherObj = SysApplication.weatherObj;
		try {
			if (weather == null || weather.equals("")) {
				return;
			}

			JSONObject dataJson = new JSONObject(weather);

			weatherObj.tmp_current = dataJson.getString("tmp_current");
			weatherObj.tmp_desc = dataJson.getString("tmp_desc");
			weatherObj.tmp_humidity = dataJson.getString("tmp_humidity");
			weatherObj.tmp_pic_from = dataJson.getString("tmp_pic_from");
			weatherObj.tmp_pic_to = dataJson.getString("tmp_pic_to");
			weatherObj.tmp_today = dataJson.getString("tmp_today");
			weatherObj.tmp_wind = dataJson.getString("tmp_wind");
		} catch (JSONException e) {
			e.printStackTrace();
			weatherObj.tmp_current = "";
			weatherObj.tmp_desc = "";
			weatherObj.tmp_humidity = "";
			weatherObj.tmp_pic_from = "";
			weatherObj.tmp_pic_to = "";
			weatherObj.tmp_today = "";
			weatherObj.tmp_wind = "";
		}
	}

	public void parsonJsonAll(String content) {
		OfflineFolder = HTTPUrl.OfflinePackage + "/";
		try {
			if (content == null || content.equals("")) {
				return;
			}
			JSONObject dataJson = new JSONObject(content);
			ContentObj contentObj = SysApplication.contentObj;
			contentObj.title = dataJson.getString("name");
			contentObj.slug = dataJson.getString("slug");
			contentObj.isFree = dataJson.getBoolean("is_free");
			contentObj.slug_icon = isOfflineData ? OfflineFolder
					+ dataJson.getString("slug_icon") : dataJson
					.getString("slug_icon");
			contentObj.guide_icon = isOfflineData ? OfflineFolder
					+ dataJson.getString("icon") : dataJson.getString("icon");
			contentObj.guide_grey_icon = isOfflineData ? OfflineFolder
					+ dataJson.getString("grey_icon") : dataJson
					.getString("grey_icon");
			contentObj.guide_color_icon = isOfflineData ? OfflineFolder
					+ dataJson.getString("colour_icon") : dataJson
					.getString("colour_icon");

			JSONArray arrayJsonRoutes = dataJson.getJSONArray("routes");
			parsonJsonRoutes(arrayJsonRoutes);

			JSONArray arrayJsonSlides = dataJson.getJSONArray("slides");
			parsonJsonSlides(arrayJsonSlides);

			JSONArray arrayJsonVideos = dataJson.getJSONArray("videos");
			parsonJsonVideos(arrayJsonVideos);

			JSONArray arrayJsonNotes = dataJson.getJSONArray("notes");
			parsonJsonNotes(arrayJsonNotes);

			JSONArray arrayJsonVisits = dataJson.getJSONArray("visits");
			parsonJsonVisits(arrayJsonVisits);

		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	public void parsonJsonInfo(String info, Boolean isOffData) {
		try {
			if (info == null || info.equals("")) {
				return;
			}

			JSONObject dataJson = new JSONObject(info);

			JSONArray arrayJsonLarks = dataJson.getJSONArray("larks");
			parsonJsonLarks(arrayJsonLarks, isOffData);

			JSONArray arrayJsonAdverts = dataJson.getJSONArray("adverts");
			parsonJsonAdverts(arrayJsonAdverts, isOffData);

			JSONObject jsonObjectInfos = dataJson.getJSONObject("informs");
			ParseJsonInfos(jsonObjectInfos, isOffData);

		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	public void parsonJsonSlides(JSONArray arrayJson) {
		try {
			for (int i = 0; i < arrayJson.length(); i++) {
				PictureObj pictureObj = new PictureObj();
				JSONObject dataJson;

				dataJson = arrayJson.getJSONObject(i);
				if (dataJson == null) {
					continue;
				}
				pictureObj.imageUrl = isOfflineData ? OfflineFolder
						+ dataJson.getString("image") : dataJson
						.getString("image");
				pictureObj.imageDescription = dataJson.getString("description");
				sys.addSlides(pictureObj);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	public void parsonJsonVideos(JSONArray arrayJson) {
		try {
			for (int i = 0; i < arrayJson.length(); i++) {
				VideosMediaObj videoObj = new VideosMediaObj();
				JSONObject dataJson;

				dataJson = arrayJson.getJSONObject(i);
				if (dataJson == null) {
					continue;
				}
				videoObj.mediaUrl = isOfflineData ? OfflineFolder
						+ dataJson.getString("video") : dataJson
						.getString("video");
				videoObj.duration = dataJson.getInt("duration");
				videoObj.size = dataJson.getInt("size");
				videoObj.name = dataJson.getString("name");
				videoObj.cover = isOfflineData ? OfflineFolder
						+ dataJson.getString("cover") : dataJson
						.getString("cover");
				sys.addVideos(videoObj);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	public void parsonJsonLarks(JSONArray arrayJson, boolean isOffData) {
		try {
			for (int i = 0; i < arrayJson.length(); i++) {
				LarkObj larkObj = new LarkObj();
				JSONObject dataJson;

				dataJson = arrayJson.getJSONObject(i);
				if (dataJson == null) {
					continue;
				}
				larkObj.lark_id = dataJson.getInt("id");
				larkObj.is_button = dataJson.getBoolean("is_button");
				larkObj.title = dataJson.getString("title");
				larkObj.validity = dataJson.getString("validity");
				larkObj.phone = dataJson.getString("phone");
				larkObj.address = dataJson.getString("address");
				larkObj.desc = dataJson.getString("desc");
				larkObj.lark_icon = isOffData ? OfflineFolder
						+ dataJson.getString("icon") : dataJson
						.getString("icon");
				String[] latLon = dataJson.getString("android_position").split(
						",");
				if (latLon.length >= 2) {
					larkObj.latitude = Double.parseDouble(latLon[1]);
					larkObj.longitude = Double.parseDouble(latLon[0]);
				}
				sys.addLarks(larkObj);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	public void parsonJsonAdverts(JSONArray arrayJson, boolean isOffData) {
		try {
			for (int i = 0; i < arrayJson.length(); i++) {
				AdvertsObj advertsObj = new AdvertsObj();
				JSONObject dataJson;

				dataJson = arrayJson.getJSONObject(i);
				if (dataJson == null) {
					continue;
				}
				advertsObj.lark_id = dataJson.getInt("lark_id");
				advertsObj.title = dataJson.getString("title");
				advertsObj.type_cd = dataJson.getInt("type_cd");
				advertsObj.url = isOffData ? OfflineFolder
						+ dataJson.getString("url") : dataJson.getString("url");
				advertsObj.advert_icon = isOffData ? OfflineFolder
						+ dataJson.getString("icon") : dataJson
						.getString("icon");
				sys.addAdverts(advertsObj);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	public void parsonJsonNotes(JSONArray arrayJson) {
		try {
			for (int i = 0; i < arrayJson.length(); i++) {
				NotesListObj noteObj = new NotesListObj();
				JSONObject dataJson;

				dataJson = arrayJson.getJSONObject(i);
				if (dataJson == null) {
					continue;
				}
				noteObj.title = dataJson.getString("title");
				noteObj.subTitle = dataJson.getString("subtitle");
				noteObj.author = dataJson.getString("author");
				noteObj.source = dataJson.getString("source");
				noteObj.time = dataJson.getString("time");
				noteObj.icon = isOfflineData ? OfflineFolder
						+ dataJson.getString("icon") : dataJson
						.getString("icon");

				JSONArray arrayArticle = dataJson.getJSONArray("articles");
				noteObj.detailedsArticles = ParseJsonArticle(arrayArticle,
						isOfflineData);
				sys.addNotesList(noteObj);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	public void ParseJsonInfos(JSONObject jsonObject, boolean isOffData) {
		List<InfosListObj> NewsList = new ArrayList<InfosListObj>();
		List<InfosListObj> TipsList = new ArrayList<InfosListObj>();
		try {
			JSONArray arrayNews = jsonObject.getJSONArray("message");
			for (int i = 0; i < arrayNews.length(); i++) {
				JSONObject jsonNews = arrayNews.getJSONObject(i);
				InfosListObj infoNewsList = new InfosListObj();
				infoNewsList.title = jsonNews.getString("title");
				infoNewsList.source = jsonNews.getString("source");
				infoNewsList.time = jsonNews.getString("time");
				infoNewsList.tip_type_cd = jsonNews.getInt("tip_type_cd");
				JSONArray arrayArticle = jsonNews.getJSONArray("articles");
				infoNewsList.detailedsInfosList = ParseJsonArticle(
						arrayArticle, isOffData);
				NewsList.add(infoNewsList);
			}
			sys.addInfosList("news", NewsList);

			JSONArray arrayTips = jsonObject.getJSONArray("tip");
			for (int i = 0; i < arrayTips.length(); i++) {
				JSONObject jsonTips = arrayTips.getJSONObject(i);
				InfosListObj infoTipsList = new InfosListObj();
				infoTipsList.title = jsonTips.getString("title");
				infoTipsList.source = jsonTips.getString("source");
				infoTipsList.time = jsonTips.getString("time");
				infoTipsList.tip_type_cd = jsonTips.getInt("tip_type_cd");
				JSONArray arrayArticle = jsonTips.getJSONArray("articles");
				infoTipsList.detailedsInfosList = ParseJsonArticle(
						arrayArticle, isOffData);
				TipsList.add(infoTipsList);
			}
			sys.addInfosList("tips", TipsList);

		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	public void parsonJsonVisits(JSONArray arrayJson) {
		try {
			for (int i = 0; i < arrayJson.length(); i++) {
				VisitsListObj visitListObj = new VisitsListObj();
				JSONObject dataJson;

				dataJson = arrayJson.getJSONObject(i);
				if (dataJson == null) {
					continue;
				}
				visitListObj.name = dataJson.getString("name");
				visitListObj.visit_icon = isOfflineData ? OfflineFolder
						+ dataJson.getString("icon") : dataJson
						.getString("icon");
				// JSONArray visitArray = dataJson.getJSONArray("sites");

				JSONArray arrayVisits = dataJson.getJSONArray("sites");
				visitListObj.detailedsSites = ParsonJsonSites(arrayVisits, i);
				sys.addVisitsList(visitListObj);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	public List<ArticleListObj> ParsonJsonSites(JSONArray arrayJson, int index) {
		List<ArticleListObj> articleList = new ArrayList<ArticleListObj>();

		try {
			for (int i = 0; i < arrayJson.length(); i++) {
				JSONObject dataJson;
				dataJson = arrayJson.getJSONObject(i);
				ArticleListObj articleListObj = new ArticleListObj();
				articleListObj.title = dataJson.getString("title");
				articleListObj.subTitle = dataJson.getString("subtitle");
				articleListObj.distance = dataJson.getString("distance");
				articleListObj.spendTime = dataJson.getString("spend_time");
				articleListObj.description = dataJson.getString("desc");
				articleListObj.icon = isOfflineData ? OfflineFolder
						+ dataJson.getString("cover") : dataJson
						.getString("cover");
				articleListObj.type_cd = dataJson.getInt("type_cd");

				JSONArray arrayArticles = dataJson.getJSONArray("articles");
				articleListObj.detailedsArticleList = ParseJsonArticle(
						arrayArticles, isOfflineData);
				articleList.add(articleListObj);
				// sys.articlesListObj.add(articleListObj);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return articleList;
	}

	public List<ArticleObj> ParseJsonArticle(JSONArray arrayJson,
			boolean isOfflineData) {
		List<ArticleObj> articleList = new ArrayList<ArticleObj>();
		try {
			for (int i = 0; i < arrayJson.length(); i++) {
				JSONObject dataJson;
				dataJson = arrayJson.getJSONObject(i);

				ArticleObj articleObj = new ArticleObj();
				articleObj.title = dataJson.getString("title");
				articleObj.description = dataJson.getString("desc");
				if (!dataJson.getString("icon").equals("")) {
					articleObj.icon = isOfflineData ? OfflineFolder
							+ dataJson.getString("icon") : dataJson
							.getString("icon");
				} else {
					articleObj.icon = "";
				}
				if (!dataJson.getString("thumb_icon").equals("")) {
					articleObj.thumb_icon = isOfflineData ? OfflineFolder
							+ dataJson.getString("thumb_icon") : dataJson
							.getString("thumb_icon");
				} else {
					articleObj.thumb_icon = "";
				}
				articleList.add(articleObj);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return articleList;
	}

	public void parsonJsonRoutes(JSONArray arrayJson) {
		try {
			for (int i = 0; i < arrayJson.length(); i++) {
				RouteObj routeObj = new RouteObj();
				JSONObject dataJson;

				dataJson = arrayJson.getJSONObject(i);
				if (dataJson == null) {
					continue;
				}
				routeObj.index = i;
				routeObj.routeUrl = isOfflineData ? OfflineFolder
						+ dataJson.getString("icon") : dataJson
						.getString("icon");
				// routeObj.routeUrl =
				// "http://172.27.3.6:3000/uploads/image/c3616c356c3ef3a69381d80854ec2d6f.jpg";
				JSONArray arrayPoints = dataJson.getJSONArray("points");
				routeObj.pointList = ParsonJsonPoints(arrayPoints);
				sys.addRoutesList(routeObj);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	public List<RoutePointObj> ParsonJsonPoints(JSONArray arrayJson) {
		List<RoutePointObj> pointsList = new ArrayList<RoutePointObj>();

		try {
			for (int i = 0; i < arrayJson.length(); i++) {
				JSONObject dataJson;
				dataJson = arrayJson.getJSONObject(i);
				RoutePointObj pointObj = new RoutePointObj();
				pointObj.index = i;
				pointObj.name = dataJson.getString("name");
				String[] latLon = dataJson.getString("android_position").split(
						",");
				if (latLon.length >= 2) {
					pointObj.latitude = Double.parseDouble(latLon[1]);
					pointObj.longitude = Double.parseDouble(latLon[0]);
				}

				String[] ptPosition = dataJson.getString("coord").split(",");

				if (ptPosition.length >= 2) {

					pointObj.ptPos = new Point(
							(int) (Float.parseFloat(ptPosition[0])),
							(int) (Float.parseFloat(ptPosition[1])));
				}

				pointObj.typeID = dataJson.getInt("type_cd");

				JSONArray arrayAudios = dataJson.getJSONArray("audios");
				pointObj.audioList = ParseJsonPointAudios(arrayAudios);
				pointsList.add(pointObj);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return pointsList;
	}

	public List<PointAudioObj> ParseJsonPointAudios(JSONArray arrayJson) {
		List<PointAudioObj> audiosList = new ArrayList<PointAudioObj>();
		try {
			for (int i = 0; i < arrayJson.length(); i++) {
				JSONObject dataJson;
				dataJson = arrayJson.getJSONObject(i);

				PointAudioObj audioObj = new PointAudioObj();
				audioObj.index = i;
				audioObj.audioUrl = isOfflineData ? OfflineFolder
						+ dataJson.getString("audio") : dataJson
						.getString("audio");
				audioObj.size = dataJson.getString("size");
				audioObj.duration = dataJson.getString("duration");
				audioObj.name = dataJson.getString("name");
				audiosList.add(audioObj);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return audiosList;
	}

}
